package com.yeyuanqi.hottop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yeyuanqi.hottop.annotation.AccessLimit;
import com.yeyuanqi.hottop.common.Result;
import com.yeyuanqi.hottop.entity.TBaiduhot;
import com.yeyuanqi.hottop.entity.TJinrihot;
import com.yeyuanqi.hottop.entity.TTxhot;
import com.yeyuanqi.hottop.entity.TWeibohot;
import com.yeyuanqi.hottop.service.TBaiduhotService;
import com.yeyuanqi.hottop.service.TJinrihotService;
import com.yeyuanqi.hottop.service.TTxhotService;
import com.yeyuanqi.hottop.service.TWeibohotService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 热搜标题搜索控制器
 * @author yeyuanqi
 * @since 2022-02-20
 */
@RestController
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private TBaiduhotService tBaiduhotService;

    @Autowired
    private TJinrihotService tJinrihotService;

    @Autowired
    private TTxhotService tTxhotService;

    @Autowired
    private TWeibohotService tWeibohotService;


    /**
     * 根据热搜标题搜索热搜，并只显示2条
     */
    @GetMapping()
    public Result search(@RequestParam String queryString) {
        List<TBaiduhot> bdlist = tBaiduhotService.list(new QueryWrapper<TBaiduhot>()
                .like("topic", queryString).orderByDesc("id","ranking").last("limit 1"));
        List<TWeibohot> wblist = tWeibohotService.list(new QueryWrapper<TWeibohot>()
                .like("topic", queryString).orderByDesc("id","ranking").last("limit 1"));
        List<TTxhot> txlist = tTxhotService.list(new QueryWrapper<TTxhot>()
                .like("topic", queryString).orderByDesc("id","ranking").last("limit 1"));
        List<TJinrihot> jrlist = tJinrihotService.list(new QueryWrapper<TJinrihot>()
                .like("topic", queryString).orderByDesc("id","ranking").last("limit 1"));
        List<Object> list = new ArrayList<>();
        if (bdlist.size()!=0){
            list.add(bdlist.get(0));
        }
        if (wblist.size()!=0){
            list.add(wblist.get(0));
        }
        if (txlist.size()!=0){
            list.add(txlist.get(0));
        }
        if (jrlist.size()!=0){
            list.add(jrlist.get(0));
        }
        if (list.size()!=0){
            return Result.succ(list);
        }else {
            return Result.fail("无搜索结果");
        }

    }

    /**
     * 根据热搜标题搜索所有热搜
     */
    @RequiresAuthentication
    @AccessLimit(seconds = 30, maxCount = 20, msg = "30秒内一个平台只能搜索20次")
    @GetMapping("/wb")
    public Result searchwb(@RequestParam(defaultValue = "1") Integer currentPage,@RequestParam(defaultValue = "10") Integer pageSize,@RequestParam String queryString) {
        Page page = new Page(currentPage, pageSize);
        IPage wblist = tWeibohotService.page(page,new QueryWrapper<TWeibohot>().like("topic", queryString).orderByDesc("id"));
        return Result.succ(wblist);
    }
    /**
     * 根据热搜标题搜索所有热搜
     */
    @RequiresAuthentication
    @AccessLimit(seconds = 30, maxCount = 20, msg = "30秒内一个平台只能搜索20次")
    @GetMapping("/bd")
    public Result searchbd(@RequestParam(defaultValue = "1") Integer currentPage,@RequestParam(defaultValue = "10") Integer pageSize,@RequestParam String queryString) {
        Page page = new Page(currentPage, pageSize);
        IPage bdlist = tBaiduhotService.page(page,new QueryWrapper<TBaiduhot>().like("topic", queryString).orderByDesc("id"));
        return Result.succ(bdlist);
    }

    /**
     * 根据热搜标题搜索所有热搜
     */
    @RequiresAuthentication
    @AccessLimit(seconds = 30, maxCount = 20, msg = "30秒内一个平台只能搜索20次")
    @GetMapping("/jr")
    public Result searchjr(@RequestParam(defaultValue = "1") Integer currentPage,@RequestParam(defaultValue = "10") Integer pageSize,@RequestParam String queryString) {
        Page page = new Page(currentPage, pageSize);
        IPage jrlist = tJinrihotService.page(page,new QueryWrapper<TJinrihot>().like("topic", queryString).orderByDesc("id"));
        return Result.succ(jrlist);
    }

    /**
     * 根据热搜标题搜索所有热搜
     */
    @RequiresAuthentication
    @AccessLimit(seconds = 30, maxCount = 20, msg = "30秒内一个平台只能搜索20次")
    @GetMapping("/tx")
    public Result searchtx(@RequestParam(defaultValue = "1") Integer currentPage,@RequestParam(defaultValue = "10") Integer pageSize,@RequestParam String queryString) {
        Page page = new Page(currentPage, pageSize);
        IPage txlist = tTxhotService.page(page,new QueryWrapper<TTxhot>().like("topic", queryString).orderByDesc("id"));
        return Result.succ(txlist);
    }
}
